//
//  JEHVideoViewController.m
//  SinaNews
//
//  Created by 蒋恩浩 on 15/12/1.
//  Copyright © 2015年 JEH. All rights reserved.
//

#import "JEHVideoViewController.h"

@interface JEHVideoViewController ()< UICollectionViewDelegate, UICollectionViewDataSource, playVideo>

@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *dataSource;
@property (nonatomic, retain) NSArray *titleUrlArray;
@property (nonatomic, retain) NSArray *nameArray;
@property (nonatomic, retain) NSMutableArray *buttonArray;
@property (nonatomic, retain) UIView *headerView;

@end

@implementation JEHVideoViewController

- (void)dealloc {
    
    [_collectionView release];
    [_dataSource release];
    [_titleUrlArray release];
    [_nameArray release];
    [_buttonArray release];
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.view nightMode];
    [self.collectionView nightMode];
    [self.collectionView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createHeaderTitle];
    [self createCollectionView];
}

#pragma mark - createHeaderTitle
- (void)createHeaderTitle {
    
    self.buttonArray = [NSMutableArray arrayWithCapacity:0];
    self.titleUrlArray = @[kLaughGETURL, kShockGETURL, kSweetGETURL, kGossipGETURL];
    self.nameArray = @[@"笑cry", @"震惊", @"暖心", @"八卦"];
    CGFloat width = self.view.width / 6;
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 20 + kHeaderHeight)];
    [_headerView release];
    for (int i = 0; i < 4; i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        [button setTitle:_nameArray[i] forState:UIControlStateNormal];
        button.frame = CGRectMake(i * width + width, 20, width, kHeaderHeight);
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        if (i == 0) {
            [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        }
        [button addTarget:self action:@selector(headerButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = 100 + i;
        [self.headerView addSubview:button];
        [self.buttonArray addObject:button];
    }
    [self.view addSubview:self.headerView];
}

#pragma mark - headerButtonAction
- (void)headerButtonAction:(UIButton *)sender {
    
    [self.collectionView setContentOffset:CGPointMake(self.collectionView.width * (sender.tag - 100), 0)];
    for (int i = 0; i < _buttonArray.count; i++) {
        
        if (sender.tag - 100 == i) {
            
            [sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        } else {
            [_buttonArray[i] setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        }
    }
}

#pragma mark - scroll
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    NSInteger temp = self.collectionView.contentOffset.x / _collectionView.width;
    [self headerButtonAction:_buttonArray[temp]];
}

#pragma mark - createCollectionView
- (void)createCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(self.view.width, self.view.height - kHeaderHeight - 44 - 5 - 20);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, kHeaderHeight + 20, self.view.width, self.view.height - kHeaderHeight - 44 - 5 - 20) collectionViewLayout:layout];
    [_collectionView release];
    [layout release];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.pagingEnabled = YES;
    _collectionView.bounces = NO;
    [_collectionView registerClass:[JEHVideoCollectionViewCell class] forCellWithReuseIdentifier:@"JEHVideoCollectionViewCellIdentifier"];
    [self.view addSubview:self.collectionView];
}

#pragma mark - UICollectionViewDelegateMethod
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (_titleUrlArray) {
        
        return self.titleUrlArray.count;
    }
    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    JEHVideoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"JEHVideoCollectionViewCellIdentifier" forIndexPath:indexPath];
    if (indexPath.item < self.titleUrlArray.count) {
        
        cell.url = self.titleUrlArray[indexPath.item];
        cell.delegate = self;
    }
    return cell;
}

#pragma mark - playVideo
- (void)playVideoWithUrl:(NSString *)url {

    JEHSinglePlayerViewController *playerVC = [JEHSinglePlayerViewController shareSinglePlayerController];
    playerVC.player = [[AVPlayer alloc] initWithURL:[NSURL URLWithString:url]];
    [playerVC.player play];
    [self presentViewController:playerVC animated:YES completion:^{
        
    }];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
